package com.hxkj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.ArticleTagsDao;
import com.hxkj.dao.TagsDao;
import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.Tags;
import com.hxkj.domain.enums.TagsExceptionEnum;
import com.hxkj.service.TagsService;
import com.hxkj.utils.HanyuPinyinHelper;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TagsServiceImpl
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/7 11:59
 * @Version 1.0
 */

@Service
@Slf4j
public class TagsServiceImpl implements TagsService{

    @Autowired
    private TagsDao tagsDao;
    @Autowired
    private ArticleTagsDao articleTagsDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct saveTags(Tags tags) throws Exception{
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            //Mr.zheng:对比tags名称是否已存在
            if (tagsDao.getTagsByName(tags.getTagsName(),tags.getSiteId()).size()!=0){
                resultMessageStruct.setCode(TagsExceptionEnum.TAGS_IS_EXIST.getCode());
                resultMessageStruct.setMessage(TagsExceptionEnum.TAGS_IS_EXIST.getMsg());
                return resultMessageStruct;
            }
            tagsDao.saveTags(tags);
            resultMessageStruct.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(TagsExceptionEnum.TAGS_SAVE_ERROE.getCode());
            resultMessageStruct.setMessage(TagsExceptionEnum.TAGS_SAVE_ERROE.getMsg());
            throw e;
        }
        return resultMessageStruct;
    }

    @Override
    public PageInfo<Tags> getTagsList(Tags tags, PageTo pageTo) throws Exception{
        Page<Tags> page = PageHelper.startPage(pageTo.getPageNum(), pageTo.getPageSize());
        PageInfo<Tags> pageInfo = new PageInfo<>();
        List<Tags> tagsList;
        try {
            tagsList = tagsDao.getTagslist(tags);

            pageInfo = new PageInfo<>(page);
            pageInfo.setList(tagsList);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取标签列表失败");
        }
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct removeTags(Tags tags) throws Exception{
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            //Mr.zheng:切割id字符串
            String[] tagsIds = tags.getTagsIds().split(",");
            //Mr.zheng:遍历删除
            for (int i = 0; i < tagsIds.length; i++) {
                tagsDao.removeTagsById(Long.valueOf(tagsIds[i]));
                //Mr.zheng:删除与其关联的中间表数据

                articleTagsDao.removeArticleTagsByTagsId(Long.valueOf(tagsIds[i]));
            }
            resultMessageStruct.setCode(200);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            resultMessageStruct.setCode(TagsExceptionEnum.TAGS_DELETE_ERROE.getCode());
            resultMessageStruct.setMessage(TagsExceptionEnum.TAGS_DELETE_ERROE.getMsg());
            throw e;
        }
        return resultMessageStruct;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct modifyTags(Tags tags) throws Exception{
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            Tags tag = tagsDao.getTagsById(tags.getTagsId());
            if(null == tag) {
                resultMessageStruct.setCode(TagsExceptionEnum.TAGS_UPDATE_ERROE.getCode());
                resultMessageStruct.setMessage(TagsExceptionEnum.TAGS_UPDATE_ERROE.getMsg());
                return resultMessageStruct;
            }
            //Mr.zheng:查看标签名称是否修改
            if (tag.getTagsName().equals(tags.getTagsName())){
                tagsDao.modifyTags(tags);
                resultMessageStruct.setCode(200);
                return resultMessageStruct;
            }
            //Mr.zheng:查看名字是否重复
            if (tagsDao.getTagsByName(tags.getTagsName(),tags.getSiteId()).size()==0) {
                tagsDao.modifyTags(tags);
                resultMessageStruct.setCode(200);
                return resultMessageStruct;
            }
            resultMessageStruct.setCode(TagsExceptionEnum.TAGS_IS_EXIST.getCode());
            resultMessageStruct.setMessage(TagsExceptionEnum.TAGS_IS_EXIST.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(TagsExceptionEnum.TAGS_UPDATE_ERROE.getCode());
            resultMessageStruct.setMessage(TagsExceptionEnum.TAGS_UPDATE_ERROE.getMsg());
            throw e;
        }
        return resultMessageStruct;
    }

    @Override
    public List<Tags> getShowTagsList(Tags tags) throws Exception{
        List<Tags> tagsList = new ArrayList<>();
        try {
            tagsList = tagsDao.getShowTagsList(tags);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取标签列表失败");
        }
        return tagsList;
    }

    @Override
    public List<Tags> getQuoteCountTagsList(Tags tags) throws Exception{
        List<Tags> tagsList = new ArrayList<>();
        try {
            tagsList = tagsDao.getQuoteCountTagsList(tags);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取标签列表失败");
        }
        return tagsList;
    }

    @Override
    public List<Tags> getTagsListByQuoteCount(Tags tags) throws Exception {
        List<Tags> tagsList = new ArrayList<>();
        try {
            tagsList = tagsDao.getTagsListByQuoteCount(tags);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取tags列表失败");
        }
        return tagsList;
    }

    @Override
    public List<Article> getArticleByTag(Tags tags) throws Exception {
        List<Article> articleList = new ArrayList<>();
        try {
            articleList = tagsDao.getArticleByTag(tags);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取文章列表失败");
        }
        return articleList;
    }

    @Override
    public PageInfo<Tags> getHotTagsList(Tags tags , PageTo pageTo) throws Exception{
        Page<Tags> page = PageHelper.startPage(pageTo.getPageNum(), pageTo.getPageSize());
        tagsDao.getHotTagsList(tags);
        PageInfo pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @Override
    public List<Tags> getHotArticleAsTagsList(Article article) throws Exception {
        return null;
    }


    @Override
    public Tags getTags(Tags tags) throws Exception {
        try {
            tags = tagsDao.getTagsById(tags.getTagsId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tags;
    }




}
